package com.qianfeng.xinyihezi.xinyihezi.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.GridView;

import com.google.gson.Gson;
import com.qianfeng.xinyihezi.xinyihezi.R;
import com.qianfeng.xinyihezi.xinyihezi.adapter.MyGoodBeanAdapter;
import com.qianfeng.xinyihezi.xinyihezi.config.MyNetConfig;
import com.qianfeng.xinyihezi.xinyihezi.config.MyRetrofit;
import com.qianfeng.xinyihezi.xinyihezi.model.GoodBean;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * 作者：王加威 on 2016/11/14 16:45
 */

public class ZhuTiFragment extends Fragment {

    private GridView mGridView;
    private StaggeredGridLayoutManager mManager;
    private String mTitle;
    private String mGods_id;
    private List<GoodBean.DataBean> mDataBeen;
    private Bundle mBundle;
    private int page=1;
    private boolean isload;
    private MyGoodBeanAdapter mAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.zhuti_fragment, null);

        mBundle = getArguments();
        mTitle=null;
        mGods_id=null;
        mTitle = mBundle.getString("title");
        mGods_id = mBundle.getString("gods_id");
        Log.e("gods_id", mGods_id);
        initView(view);
        loaData(Integer.valueOf(mGods_id),page);
        setData();
        setListener();
        return view;
    }

    private void setListener() {
        mGridView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

                if (isload && scrollState==SCROLL_STATE_IDLE) {
                    page++;
                    if (mTitle.equals("全部")) {
                        loaData(Integer.valueOf(mGods_id),page);
                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                isload=(firstVisibleItem+visibleItemCount)==totalItemCount;
            }
        });
    }


    private void initView(View view) {
        mGridView = (GridView) view.findViewById(R.id.zhuti_fragment_RecyclerView);
        mDataBeen = new ArrayList<>();
    }

    private void loaData(int id,int page) {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(MyNetConfig.BASE_PATH).build();
        MyRetrofit myRetrofit = retrofit.create(MyRetrofit.class);
        myRetrofit.getGoodsData(id,page).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response.isSuccessful()) {
                    try {
                       GoodBean mBean = new Gson().fromJson(response.body().string(), GoodBean.class);
                        mDataBeen.addAll(mBean.data);
                        mAdapter.notifyDataSetChanged();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }

    private void setData() {
        mAdapter = new MyGoodBeanAdapter(getActivity(), mDataBeen);
        mGridView.setAdapter(mAdapter);
    }
}
